﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    public static class EntityFrameworkExtensions
    {
        /// <summary>
        /// 在集合中添加多个元素
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="collection">源集合</param>
        /// <param name="itemsToAdd">欲添加的项目集合</param>
        public static void AddMany<TModel>(this ICollection<TModel> collection, IEnumerable<TModel> itemsToAdd)
        {
            foreach (TModel item in itemsToAdd)
                collection.Add(item);
        }
        /// <summary>
        /// 删除集合中多个元素
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="collection">源集合</param>
        /// <param name="itemsToDelete">要删除的项目集合</param>
        /// <returns>如果删除全部成功返回true，否则返回false</returns>
        public static bool RemoveMany<TModel>(this ICollection<TModel> collection, IEnumerable<TModel> itemsToDelete)
        {
            bool success = true;
            foreach (TModel item in itemsToDelete)
            {
                bool result = collection.Remove(item);
                if (!result)
                    success = false;
            }
            return success;
        }
    }
}
